package com.carrental.search.controller;

import com.carrental.common.result.Result;
import com.carrental.search.service.DataSyncService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 数据同步控制器
 * 提供手动触发数据同步的API接口
 */
@Tag(name = "数据同步接口", description = "提供手动触发数据同步的API接口")
@RestController
@RequestMapping("/sync")
public class DataSyncController {

    @Resource
    private DataSyncService dataSyncService;

    /**
     * 全量同步车辆数据
     *
     * @return 同步结果
     */
    @Operation(summary = "全量同步车辆数据", description = "从车辆微服务获取全部车辆数据，并更新到Elasticsearch")
    @PostMapping("/vehicles/full")
    public Result<Integer> syncAllVehicles() {
        int count = dataSyncService.syncAllVehicles();
        return Result.success( "成功同步" + count + "条车辆数据",count);
    }

    /**
     * 增量同步车辆数据
     *
     * @param lastSyncTime 上次同步时间（时间戳）
     * @return 同步结果
     */
    @Operation(summary = "增量同步车辆数据", description = "从车辆微服务获取最近更新的车辆数据，并更新到Elasticsearch")
    @PostMapping("/vehicles/incremental")
    public Result<Integer> syncIncrementalVehicles(
            @Parameter(description = "上次同步时间（时间戳）", required = true)
            @RequestParam Long lastSyncTime) {
        int count = dataSyncService.syncIncrementalVehicles(lastSyncTime);
        return Result.success( "成功增量同步" + count + "条车辆数据",count);
    }

    /**
     * 同步单个车辆数据
     *
     * @param vehicleId 车辆ID
     * @return 同步结果
     */
    @Operation(summary = "同步单个车辆数据", description = "从车辆微服务获取指定车辆数据，并更新到Elasticsearch")
    @PostMapping("/vehicle/{vehicleId}")
    public Result<Boolean> syncVehicle(
            @Parameter(description = "车辆ID", required = true)
            @PathVariable Long vehicleId) {
        boolean success = dataSyncService.syncVehicle(vehicleId);
        if (success) {
            return Result.success("成功同步车辆数据",true );
        } else {
            return Result.error("同步车辆数据失败");
        }
    }

    /**
     * 删除车辆索引数据
     *
     * @param vehicleId 车辆ID
     * @return 删除结果
     */
    @Operation(summary = "删除车辆索引数据", description = "删除Elasticsearch中的车辆索引数据")
    @DeleteMapping("/vehicle/{vehicleId}")
    public Result deleteVehicle(
            @Parameter(description = "车辆ID", required = true)
            @PathVariable Long vehicleId) {
        boolean success = dataSyncService.deleteVehicle(vehicleId);
        if (success) {
            return Result.success( "成功删除车辆索引数据");
        } else {
            return Result.error("删除车辆索引数据失败");
        }
    }

    /**
     * 同步门店位置数据
     *
     * @return 同步结果
     */
    @Operation(summary = "同步门店位置数据", description = "从门店微服务获取门店位置数据，用于地理位置搜索")
    @PostMapping("/store-locations")
    public Result<Integer> syncStoreLocations() {
        int count = dataSyncService.syncStoreLocations();
        return Result.success("成功同步" + count + "条门店位置数据",count);
    }
} 